﻿using com.shinian.fcg.fbs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CFx : MonoBehaviour, IFx
{
    #region field
    private Animator m_Animator;
    private ParticleSystem m_ParticleSystem;
    private Transform m_Transform;
    private Vector3 m_StartLocalScale;
    private Quaternion m_StartLocalRotation;
    private Vector3 m_StartLocalPosition;
    private float m_LastScale = 0;

    private bool m_bRecycled;
    private string m_fxName;
    private Object m_fxOwner;
    private FxData? m_fxData;
    #endregion field

    #region Properties
    public bool IsPlaying
    {
        get
        {
            return gameObject.activeInHierarchy;
        }
    }

    public bool IsRecycled
    {
        get
        {
            return m_bRecycled;
        }
        set
        {
            m_bRecycled = value;
        }
    }
    public FxData FxDataInfo
    {
        get
        {
            return m_fxData.Value;
        }
        set
        {
            m_fxData = value;
        }
    }
    #endregion Properties

    public bool Init(Transform t)
    {
        if (t == null)
            return false;
        m_Animator = t.GetComponent<Animator>();
        m_ParticleSystem = t.GetComponent<ParticleSystem>();
        m_Transform = t;
        m_StartLocalPosition = t.localPosition;
        m_StartLocalRotation = t.localRotation;
        m_StartLocalScale = t.localScale;
        m_fxName = this.gameObject.name;
        if (m_ParticleSystem != null)
        {

        }
        return true;
    }

    public void SetScale(float scale)
    {
        if (m_Transform == null)
            return;
        if (m_LastScale == scale)
            return;
        m_LastScale = scale;

        //m_Transform.localScale = m_StartLocalScale * scale;
        m_Transform.localScale = new Vector3(scale, scale, scale);
    }

    public string GetName()
    {
        return m_fxName;
    }

    public string GetPath()
    {
        if (m_fxData != null)
            return m_fxData.Value.Path;
        return null;
    }

    public void Reset()
    {
        m_LastScale = 0;
        SetScale(1);
    }

    public void SetPosition(Vector3 pos)
    {
        transform.position = pos;
    }

    public void SetRotation(Quaternion rot)
    {
        transform.rotation = rot;
    }

    public void SetLocalPosition(Vector3 localPos)
    {
        transform.localPosition = localPos;
    }

    public void SetLocalRotation(Quaternion rot)
    {
        transform.localRotation = rot;
    }

    public void SetParent(GameObject obj)
    {
        if (obj == null)
            return;
        transform.SetParent(obj.transform);
    }

    public Vector3 GetPosition()
    {
        return transform.position;
    }

    public void Replay()
    {
        throw new System.NotImplementedException();
    }

    public void SetActive(bool active)
    {
        gameObject.SetActive(active);
    }

    public GameObject GetGameObject()
    {
        return gameObject;
    }

    public Object GetOwner()
    {
        return m_fxOwner;
    }

    public void SetOwner(Object obj)
    {
        m_fxOwner = obj;
    }
}
